package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.ExamQuestion;
import cn.wolfcode.domain.Question;
import cn.wolfcode.mapper.ExamMapper;
import cn.wolfcode.mapper.ExamQuestionMapper;
import cn.wolfcode.mapper.QuestionMapper;
import cn.wolfcode.qo.ExamQueryObject;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private ExamQuestionMapper examQuestionMapper;



    @Override
    public Long save(Exam exam) {
        examMapper.insert(exam);
        return exam.getId();
    }

    @Override
    public void delete(Long id) {
        examMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Exam exam) {
        examMapper.updateByPrimaryKey(exam);
    }

    @Override
    public Exam get(Long id) {
        return examMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Exam> listAll() {
        return examMapper.selectAll();
    }

    @Override
    public PageInfo<Exam> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Exam> exams = examMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Exam>(exams);
    }

    @Override
    public Exam selectByExam(Long id) {
        return examMapper.selectById(id);
    }

    @Override
    public void save1(ExamQueryObject eqo,Long id) {
        //单选
        int oneNum = eqo.getOneNum();
        List<Long> i = questionMapper.selectByType(1L);
        Collections.shuffle(i);
        int num=1;
        for (int j = 0; j < oneNum; j++) {
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setQuestionId(i.get(j));
            examQuestion.setScore(eqo.getOneScore());
            examQuestion.setSequence(num++);
            examQuestion.setExamId(id);
            examQuestionMapper.insert(examQuestion);
        }
        //多选
        int twoNum = eqo.getTwoNum();
        List<Long> twoId = questionMapper.selectByType(2L);
        Collections.shuffle(twoId);
        for (int t = 0; t < twoNum; t++) {
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setQuestionId(twoId.get(t));
            examQuestion.setScore(eqo.getOneScore());
            examQuestion.setSequence(num++);
            examQuestion.setExamId(id);
            examQuestionMapper.insert(examQuestion);
        }
        //判断题
        int trueOrFalse = eqo.getTrueOrFalse();
        List<Long> tf = questionMapper.selectByType(3L);
        Collections.shuffle(tf);
        for (int f = 0; f < trueOrFalse; f++) {
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setQuestionId(tf.get(f));
            examQuestion.setScore(eqo.getOneScore());
            examQuestion.setSequence(num++);
            examQuestion.setExamId(id);
            examQuestionMapper.insert(examQuestion);
        }
    }

    @Override
    public PageResult show(Long examId,ExamQueryObject qo) {
        List<Question> oneQuestions = questionMapper.selectforlist(examId,1L);
        List<Question> twoQuestions = questionMapper.selectforlist(examId,2L);
        List<Question> tfQuestions = questionMapper.selectforlist(examId,3L);
        return new PageResult(oneQuestions,twoQuestions,tfQuestions,qo);

    }

}

